package com.slkj.shilixiaoyuanapp.ui.common.adapter;

import android.content.Context;
import android.support.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;
import com.slkj.shilixiaoyuanapp.R;
import com.slkj.shilixiaoyuanapp.entity.FileEntity;
import com.slkj.shilixiaoyuanapp.entity.code.FileCode;
import com.slkj.shilixiaoyuanapp.net.down.DownloadInfo;
import com.slkj.shilixiaoyuanapp.ui.common.BrowseFilesActivity__JumpCenter;
import com.slkj.shilixiaoyuanapp.util.UpdataFileUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * Created  on 2018-07-23
 *
 * @author 唐开阔
 * @describe 文件下载
 */
public class FileDownAdapter extends BaseQuickAdapter<FileEntity, BaseViewHolder> {
    private Context context;
    private ArrayList<FileEntity> filePaths = new ArrayList<>();

    public FileDownAdapter(Context context, @Nullable List<FileEntity> filePaths) {
        super(R.layout.item_chose_file_down, filePaths);
        this.context = context;
        this.filePaths.addAll(filePaths);

    }

    @Override
    protected void convert(BaseViewHolder helper, FileEntity item) {
        helper.setText(R.id.tv_name, item.getName())
                .setImageResource(R.id.tv_time, item.isHave() ? R.drawable.ic_file_have : R.drawable.ic_file_dwon)
                .setText(R.id.tv_size, item.getFileSize())
                .setOnClickListener(R.id.tv_time, v -> {
                    clickFile(item);
                }).itemView.setOnClickListener(v -> {
                     clickFile(item);
        });

    }

    private void clickFile(FileEntity item) {
        if (item.isHave()) {
            openFile(new File(FileCode.MY_FILE, item.getName()));
        } else {
            QMUITipDialog dialog = new QMUITipDialog.Builder(context)
                    .setIconType(QMUITipDialog.Builder.ICON_TYPE_LOADING)
                    .setTipWord("下载中")
                    .create(false);
            UpdataFileUtil.downFile(item.getPath(), FileCode.MY_FILE,item.getName())
                    .subscribe(new Observer<DownloadInfo>() {
                        @Override
                        public void onSubscribe(Disposable d) {
                            dialog.show();
                        }

                        @Override
                        public void onNext(DownloadInfo downloadInfo) {
                        }

                        @Override
                        public void onError(Throwable e) {
                            dialog.dismiss();
                        }

                        @Override
                        public void onComplete() {
                            dialog.dismiss();
                            item.setHave(true);
                            notifyDataSetChanged();
                        }
                    });
        }
    }

    public void openFile(File file) {
        BrowseFilesActivity__JumpCenter.builder(context)
                .setFileName(file.getName())
                .setFilePath(file.getPath())
                .create().go();
    }

}
